/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jule.clases;


import javax.swing.JOptionPane;
import schemacrawler.schema.Database;
import schemacrawler.schemacrawler.DatabaseConnectionOptions;
import schemacrawler.schemacrawler.InclusionRule;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaInfoLevel;
import schemacrawler.utility.SchemaCrawlerUtility;

/**
 *
 * @author luis
 */
public class dbUtil2 {

    public static String testDatabaseMysql(String host, String database, String user, String password, String port) throws Exception {
        try {

            String url = "";
            url = "jdbc:mysql://" + host + ":" + port + "/" + database;

            Database databasex = null;
            final DatabaseConnectionOptions connectionOptions = new DatabaseConnectionOptions("org.gjt.mm.mysql.Driver", url);
            connectionOptions.setUser(user);
            connectionOptions.setPassword(password);

            // Create the options
            final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
            options.setSchemaInfoLevel(SchemaInfoLevel.minimum());
            options.setShowStoredProcedures(false);
            // Sorting options
            //options.setAlphabeticalSortForTableColumns(true);


            databasex = SchemaCrawlerUtility.getDatabase(connectionOptions.createConnection(), options);
            return "";

        } catch (SchemaCrawlerException exs) {
             System.out.println("* UtilsJUnit3Test: tearDown() method");
            return exs.getMessage();
        } catch (Exception ex) {
             System.out.println("* UtilsJUnit3Test: tearDown() method");
            return ex.getMessage();
        }


    }

    public static Database getDatabaseMysql(String host, String database, String user, String password, String port) throws Exception {
        try {

            String url = "";
            url = "jdbc:mysql://" + host + ":" + port + "/" + database;

            Database databasex = null;
            final DatabaseConnectionOptions connectionOptions = new DatabaseConnectionOptions("org.gjt.mm.mysql.Driver", url);
            connectionOptions.setUser(user);
            connectionOptions.setPassword(password);

            // Create the options
            final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
            // if using schemaInfoLevel other than minimum it
            // throws an exception that cant write to temp...
            options.setSchemaInfoLevel(SchemaInfoLevel.minimum());
            options.setShowStoredProcedures(false);
            // Sorting options
            //options.setAlphabeticalSortForTableColumns(true);


            databasex = SchemaCrawlerUtility.getDatabase(connectionOptions.createConnection(), options);
            return databasex;

        } catch (Exception ex) {
            if (ex.getMessage().contains("C:\\WINDOWS\\TEMP"))
            {
              JOptionPane.showMessageDialog(null,ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
            }

            
            System.out.println(ex.getMessage());

            return null;
        }
    }

    public static String testDatabaseSqlServer(String host, String instance, String database, String user, String password, String port) throws Exception {
        try {

            String url = "";
            url = "jdbc:jtds:sqlserver://" + host + "//" + database + ";SelectMethod=cursor";
            if (!instance.equals("")) {
                url = url + ";instance=" + instance;
            }

            Database databasex = null;
            final DatabaseConnectionOptions connectionOptions = new DatabaseConnectionOptions("net.sourceforge.jtds.jdbc.Driver", url);
            connectionOptions.setUser(user);
            connectionOptions.setPassword(password);

            // Create the options
            final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
            options.setSchemaInfoLevel(SchemaInfoLevel.minimum());
            options.setShowStoredProcedures(false);

            // Sorting options
            //options.setAlphabeticalSortForTableColumns(true);
            options.setSchemaInclusionRule(new InclusionRule(database + ".dbo", InclusionRule.NONE));

            databasex = SchemaCrawlerUtility.getDatabase(connectionOptions.createConnection(), options);
            return "";

        } catch (SchemaCrawlerException exs) {
            return exs.getMessage();
        } catch (Exception ex) {
            return ex.getMessage();
        }

    }

    public static Database getDatabaseSqlServerJtds(String host, String instance, String database, String user, String password, String port) throws Exception {
        try {

            String url = "";
            url = "jdbc:jtds:sqlserver://" + host + "//" + database + ";SelectMethod=cursor";
            if (!instance.equals("")) {
                url = url + ";instance=" + instance;
            }

            // Database databasex = null;
            final DatabaseConnectionOptions connectionOptions = new DatabaseConnectionOptions("net.sourceforge.jtds.jdbc.Driver", url);
            connectionOptions.setUser(user);
            connectionOptions.setPassword(password);

            // Create the options
            final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
            options.setSchemaInfoLevel(SchemaInfoLevel.standard());
            //options.setShowStoredProcedures(false);
            // Sorting options
            // options.setAlphabeticalSortForTableColumns(true);
            options.setSchemaInclusionRule(new InclusionRule(database + ".dbo", InclusionRule.NONE));


            final Database databasex = SchemaCrawlerUtility.getDatabase(connectionOptions.createConnection(), options);
            return databasex;

        } catch (SchemaCrawlerException exs) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    public static Database getDatabaseSqlServer(String host, String instance, String database, String user, String password, String port) throws Exception {
        try {

            String url = "";
            url = "jdbc:sqlserver://" + host;
            if (!instance.equals("")) {
                url = url + "\\" + instance;
            }
            url += ";databaseName=" + database;

            Database databasex = null;
            final DatabaseConnectionOptions connectionOptions = new DatabaseConnectionOptions("com.microsoft.sqlserver.jdbc.SQLServerDriver", url);
            connectionOptions.setUser(user);
            connectionOptions.setPassword(password);

            // Create the options
            final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
            options.setSchemaInfoLevel(SchemaInfoLevel.standard());
            options.setShowStoredProcedures(false);
            // Sorting options
            //options.setAlphabeticalSortForTableColumns(true);
            options.setSchemaInclusionRule(new InclusionRule(database + ".dbo", InclusionRule.NONE));



            databasex = SchemaCrawlerUtility.getDatabase(connectionOptions.createConnection(), options);
            return databasex;

        } catch (SchemaCrawlerException exs) {
            return null;
        } catch (Exception ex) {
            return null;
        }


    }
}
